# !/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
@作者 杨阳
@时间 2018-08-14 10:14:03
@说明 t_worker表对应的routes文件
@注意事项
"""

import json

from flask import Blueprint, request

from model.TWorkerModel import TWorkerModel
from util.JsonUtil import classToDict, JsonEncoder
from util.SessionUtil import Mast_be_login_decorator

worker = Blueprint('worker', __name__, url_prefix='/worker')


@worker.route("/grid/", methods=["POST"])
@Mast_be_login_decorator
def entity_grid():
    """
    查询实体类表格形式的数据
    :return: json 字符串
    """
    current_page = request.form.get("current_page")
    group_id = request.form.get("group_id")
    search_name = request.form.get("search_name")
    search_user = request.form.get("search_user")
    search_days = request.form.get("search_days")
    search_start_create_time = request.form.get("search_start_create_time")
    search_end_create_time = request.form.get("search_end_create_time")
    return TWorkerModel.query_entity_grid(group_id, search_name, search_user, search_days, search_start_create_time,
                                          search_end_create_time, current_page)


@worker.route("/save/", methods=["POST"])
@Mast_be_login_decorator
def save_entity():
    """
    保存实体
    :return: json 字符串
    """
    id = request.form.get("id")
    name = request.form.get("name")
    user = request.form.get("user")
    days = request.form.get("days")
    if id and id != '':
        return TWorkerModel.update_entity(id, name, user, days)
    else:
        return TWorkerModel.add_entity(name, user, days)


@worker.route("/delete/<id>", methods=["DELETE"])
@Mast_be_login_decorator
def delete_entity(id):
    """
    根据id删除实体类
    :return: json 字符串
    """
    return TWorkerModel.delete_entity_byId(id)


@worker.route("/get/<id>", methods=["GET"])
@Mast_be_login_decorator
def get_entity_byId(id):
    """
    根据id获得实体
    :return: json 字符串
    """
    worker = TWorkerModel.get_entity_byId(id)
    result = {"success": True, "worker": classToDict(worker)}
    return json.dumps(result, cls=JsonEncoder)
